package com.projectsexception.festivales.schedule.view;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.projectsexception.festivales.R;
import com.projectsexception.festivales.favorites.model.Favorite;
import com.projectsexception.festivales.setup.ProjectResources;
import com.projectsexception.festivales.setup.ShowInfo;

public class StagesViewInflater extends AbstractTabScheduleViewInflater implements OnTouchListener {
    
    private static final int TIMES_SIZE = 80;
    private float oldTouchValue;
    
    public StagesViewInflater(Activity activity) {
        super(activity);
    }

    @Override
    public void inflateMainContent(Date actualDate) {
     
        ViewGroup mainContent = (ViewGroup) activity.findViewById(R.id.main_content);
        mainContent.removeAllViews();
        
        ViewFlipper vf = (ViewFlipper) inflater.inflate(R.layout.tab_schedule_view_stages, null);
        mainContent.addView(vf);
        vf.setOnTouchListener(this);        
        
        ProjectResources res = ProjectResources.getInstance(activity);
        List<ShowInfo> shows = res.getShowsInDate(actualDate);
        List<Favorite> favorites = res.getFavorites();
        
        
        // Necesitamos saber el número de escenarios
        Set<Integer> stagesSet = new HashSet<Integer>();
        for (ShowInfo showInfo : shows) {
            stagesSet.add(showInfo.getStage().getId());
        }
        
        Map<Integer, LinearLayout> stages = new HashMap<Integer, LinearLayout>();
        Map<Integer, Integer> showsPerStage = new HashMap<Integer, Integer>();
        LinearLayout l;
        View stageView;
        TextView t;
        int stagesCount = 0;
        int stagesTotal = stagesSet.size() - 1;
        for (ShowInfo showInfo : shows) {
            if (!stages.containsKey(showInfo.getStage().getId())) {
                // Nuevo excenario
                stageView = inflater.inflate(R.layout.tab_schedule_stages, null);
                vf.addView(stageView);
                if (stagesCount > 0) {
                    t = (TextView) stageView.findViewById(R.id.stages_left);
                    t.setText("< " + stagesCount);
                }
                if (stagesCount < stagesTotal) {
                    t = (TextView) stageView.findViewById(R.id.stages_right);
                    t.setText((stagesTotal - stagesCount) + " >");
                }
                l = (LinearLayout) stageView.findViewById(R.id.stages);                
                t = (TextView) stageView.findViewById(R.id.stage_title);
                t.setText(showInfo.getStage().getName());
                stages.put(showInfo.getStage().getId(), l);
                stagesCount ++;
                showsPerStage.put(showInfo.getStage().getId(), 1);
            } else {
                l = stages.get(showInfo.getStage().getId());
                showsPerStage.put(showInfo.getStage().getId(), showsPerStage.get(showInfo.getStage().getId()) + 1);
            }
            l.addView(createShowStageView(showInfo, favorites, showsPerStage.get(showInfo.getStage().getId())));
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent touchevent) {
        switch (touchevent.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                oldTouchValue = touchevent.getX();
                break;
            }
            case MotionEvent.ACTION_UP: {
                ViewFlipper vf = (ViewFlipper) activity.findViewById(R.id.layoutswitcher);
                float currentX = touchevent.getX();
                if (oldTouchValue < currentX) {
                    if (vf.getDisplayedChild() > 0) {
                        vf.setInAnimation(AnimationHelper.inFromLeftAnimation());
                        vf.setOutAnimation(AnimationHelper.outToRightAnimation());
                        vf.showPrevious();
                    }
                }
                if (oldTouchValue > currentX) {
                    if (vf.getDisplayedChild() < vf.getChildCount() - 1) {
                        vf.setInAnimation(AnimationHelper.inFromRightAnimation());
                        vf.setOutAnimation(AnimationHelper.outToLeftAnimation());
                        vf.showNext();
                    }
                }
                break;
            }
        }
        return true;
    }
    
    private View createShowStageView(ShowInfo show, List<Favorite> favorites, int showCount) {
        View stage = inflater.inflate(R.layout.tab_schedule_horizontal_sep, null);
        ViewGroup layout = (ViewGroup) stage.findViewById(R.id.horizontal_content);
        layout.setBackgroundColor(Color.parseColor(colors[showCount % 2]));
        
        layout = (ViewGroup) stage.findViewById(R.id.horizontal_content_left);
        layout.addView(createTextView(show.getStartTime()));
        layout.addView(createTextView("-"));
        layout.addView(createTextView(show.getEndTime()));
        
        layout = (ViewGroup) stage.findViewById(R.id.horizontal_content_right);
        
        LinearLayout layoutBand = createBandView(show, favorites, new int[] {20, 0, 0, 0}, null);
        layout.addView(layoutBand);
        
        return stage;
    }
    
    private TextView createTextView(String text) {
        TextView t = new TextView(activity);
        t.setLayoutParams(new ViewGroup.LayoutParams(TIMES_SIZE, ViewGroup.LayoutParams.WRAP_CONTENT));
        t.setText(text);
        t.setTextColor(Color.BLACK);
        t.setGravity(Gravity.CENTER);
        return t;
    }

}
